const {
  controller
} = require('../controllers/Controller.js');

//index.js
const Controllers = require('../controllers/Controller.js').controller;
const ControllerVip = require("../controllers/ControllerVip").controller;

const app = getApp();

var startPoint
let leftHeight = 0,
  rightHeight = 0; //分别定义左右两边的高度
let query;
import {
  InitTime
} from "../../utils/util"
Page({
  data: {
    icon1: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210909/885563688243691520.png",
    icon2: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210914/887356889128501248.png",
    yellow_bg: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210915/887756367236956160.png",
    white_bg: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210915/887756346697449472.png",
    vip_bg: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20211126/913795093343764480.png",
    globalStype: '',
    statusBarHeight: '',
    tabbar: [],
    shopId: '',
    shopDetailsdata: '',
    shopRotationImgdata: '',
    shopRotationVideo: '',
    shopGoodsPagedata: [],
    pageNumber: 1,
    pageSize: 5,
    isEnd: false,
    type: 0,
    myInformationdata: {
      vip: 0
    },
    isloading: true,
    isFixedTop: false,
    inputValue: '',
    imgheights: [],
    //图片宽度 
    imgwidth: 750,
    //默认  
    current: 0,
    navheight: 0,
    headerheight: 0,
    barheight: 0,
    dialogShow: false,
    clickMe: true,
    tabList: [

    ],
    buttonRight: 15,
    buttonBottom: 120,
    listPage: false,
    actList: [],
    redBagList: [],
    receiveOut: false,
    closeAnimate: false,
    redMoney: "",
    lat: "",
    lng: "",
    backBtn: false,
    listType: '',
    leftList: [],
    rightList: [],
    selectIndex: 0,
    tagList: [{
        name: "全部",
        type: "",
        num: 0
      },
      {
        name: "好评",
        type: "GOOD",
        num: 0
      },
      {
        name: "差评",
        type: "BAD",
        num: 0
      },
      {
        name: "有图",
        type: "IMG",
        num: 0
      },
      {
        name: "商家回复",
        type: "REPLY",
        num: 0
      },
      {
        name: "推荐",
        type: "HOT",
        num: 0
      }
    ],
    selectKey: "",
    commentList: [],
    commentNum: "",
    bulletImg: {},
    hasRedBag: false,
    imageLoaded: false,
    isAutoplay: true,
    hiddenVideoPlayImg: false,
    showVideoControls: false,
    cardList: []
  },

  // 监听轮播图视频播放
  monitorVideoPlay() {
    this.setData({
      isAutoplay: false,
      hiddenVideoPlayImg: true, //
    })
  },
  // 监听轮播图视频暂停
  monitorVideoPause() {
    this.setData({
      isAutoplay: true,
    })
  },
  // 监听轮播图视频结束
  monitorVideoEnd() {
    this.setData({
      hiddenVideoPlayImg: false,
    })
  },
  // 监听轮播图视频进入退出全屏
  fullscreenchange(event) {
    // fullScreen false退出，true进入
    this.setData({
      showVideoControls: event.detail.fullScreen,
    })
  },
  // 全屏播放
  fullScreenView() {
    if (this.data.showVideoControls) return // 为true 表示已全屏，退出函数
    this.video.requestFullScreen()
    this.setData({
      showVideoControls: true,
    })
  },
  // 播放轮播图视频
  playVideo() {
    this.video.play()
  },

  // 监听轮播图切换--非视频页停止播放视频
  swiperChange(e) {
    if (e.detail.current != 0 && this.data.shopRotationVideo) {
      this.video.pause()
      this.setData({
        isAutoplay: true,
      })
    }
  },

  //跳转拼团列表页
  toPinListPage() {
    wx.navigateTo({
      url: '/pages/Index/pinListPage',
    })
  },

  //跳转拼团详情
  toPinGoodsDetail(e) {
    wx.navigateTo({
      url: "/pages/Index/Detail?id=" + e.currentTarget.dataset.id,
    })
  },

  //跳转上传入驻信息
  toUploadSettled() {
    wx.navigateTo({
      url: '/pages/Index/uploadSettled',
    })
  },


  //获取评论数目
  getCommentNum() {
    let value = {}
    value.shopId = wx.getStorageSync('shopId')
    Controllers.commentNum(value).then(res => {
      let list = this.data.tagList
      list[0].num = res.data.totalNum || 0
      list[1].num = res.data.good || 0
      list[2].num = res.data.bad || 0
      list[3].num = res.data.img || 0
      list[4].num = res.data.reply || 0
      list[5].num = res.data.hot || 0
      this.setData({
        tagList: list
      })
    })
  },

  //切换评价
  tagSelect(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      selectKey: this.data.tagList[index].type,
      pageNumber: 1
    })
    this.getCommentList()
  },

  //获取评论列表
  getCommentList() {
    let value = {}
    value.pageNumber = this.data.pageNumber
    value.shopId = wx.getStorageSync('shopId')
    value.selectKey = this.data.selectKey
    Controllers.goodsCommentPage(value).then(res => {
      res.data.list.forEach(item => {
        if (item.commentImages) {
          item.commentImages = item.commentImages.split(",")
        }
      })
      this.setData({
        commentList: res.data.list,
        lastPage: res.data.lastPage
      })
    })
  },

  getCommentMore() {
    if (this.data.lastPage == false) {
      let value = {}
      value.pageNumber = this.data.pageNumber + 1
      value.shopId = wx.getStorageSync('shopId')
      this.setData({
        pageNumber: value.pageNumber
      })
      Controllers.goodsCommentPage(value).then(res => {
        res.data.list.forEach(item => {
          item.commentImages = item.commentImages.split(",")
        })
        let list = this.data.commentList
        let list2 = res.data.list
        let list3 = list.concat(list2)
        this.setData({
          commentList: list3,
          lastPage: res.data.lastPage
        })
      })
    }
  },


  //切换tab
  changeSelectTab(e) {

    const index = e.currentTarget.dataset.index
    // if(this.data.selectIndex==index)return//数据不刷新
    this.setData({
      selectIndex: index,
      pageNumber: 1,
      lastPage: false,
      leftList: [],
      rightList: []
    })
    leftHeight = 0
    rightHeight = 0;
    if (this.data.selectIndex == 0) {
      this.setData({
        listType: 0
      })
      this.shopGoodsPage()
    } else if (this.data.selectIndex == 1) {
      this.getCardList()
    } else if (this.data.selectIndex == 2) {
      this.setData({
        listType: 1
      })
      this.getShopActivity()
    } else {
      this.getCommentList()

    }
  },

  //获取首页活动
  getShopActivity() {
    let that = this
    Controllers.shopActivityPage({
      pageSize: 5,
      pageNumber: 1,
      shopId: this.data.shopId
    }).then(res => {
      console.log("活动列表")
      // console.log(res)
      this.setData({
        activityList: res.data.list,
        lastPage: res.data.lastPage
      })
      // 防止不同列表覆盖
      if (this.data.listType == 1) {
        that.waterfallFlow(res.data.list)
      }
    })
  },

  //跳转我要评价
  toWillComment() {
    wx.navigateTo({
      url: '/pages/Index/willComment',
    })
  },

  //获取vipCard列表
  getCardList() {
    let value = {
      shopId: this.data.shopId,
      types: '0,1,5'
    }
    ControllerVip.getVipCard(value).then((res) => {
      this.setData({
        cardList: res.data
      })
    });
  },
  //跳转vipCard详情
  jumpDetail(event) {
    let id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/my/vip/info/VIPCardInfo?id=' + id + '&notQC=1',
    });

  },

  //打开地址
  location(e) {
    var lng = this.data.shopDetailsdata.lng;
    var lat = this.data.shopDetailsdata.lat;
    wx.openLocation({
      //当前经纬度
      latitude: Number(lat),
      longitude: Number(lng),
      //缩放级别默认28
      scale: 28,
      //成功打印信息
      success: function (res) {
        console.log(res)
      },
      //失败打印信息
      fail: function (err) {
        console.log(err)
      },
      //完成打印信息
      complete: function (info) {
        console.log(info)
      },
    })
  },

  //跳转商品详情
  toShopGoodsDetail(e) {
    if (this.data.selectIndex == 0) {
      wx.navigateTo({
        url: "/pages/Index/Detail?id=" + e.currentTarget.dataset.id,
      })
    } else {
      let type = e.currentTarget.dataset.type
      let id = e.currentTarget.dataset.id
      let shopGroupId = e.currentTarget.dataset.shopgroupid
      wx.navigateTo({
        url: '/pages/TradingArea/GiftBagDetail?id=' + id + "&shopGroupId=" + shopGroupId + "&type=" + type,
      })
    }
  },

  //打电话
  calling() {
    let phone = this.data.shopDetailsdata.customer
    app.calling(phone)
  },

  //跳转商圈券包
  toMyShopCircleBag() {
    // wx.navigateTo({
    //   url: '/pages/Index/myCouponBag',
    // })
    wx.navigateTo({
      url: '/pages/Index/myShopCircleCoupon',
    })
  },

  //跳转商圈首页
  toShopCircle() {
    wx.reLaunch({
      url: '/pages/TradingArea/index',
    })
  },

  getUserMsg() {
    Controllers.getAccountDetail().then(res => {
      let timeGreet = "";
      let nowTime = new Date().getHours()
      console.log(nowTime)
      if (nowTime == 0) {
        nowTime = 24
      }
      if (nowTime < 8 && nowTime >= 5) {
        timeGreet = "早上"
      } else if (nowTime < 12 && nowTime >= 8) {
        timeGreet = "上午"
      } else if (nowTime < 14 && nowTime >= 12) {
        timeGreet = "中午"
      } else if (nowTime < 18 && nowTime >= 14) {
        timeGreet = "下午"
      } else if (nowTime < 19 && nowTime >= 18) {
        timeGreet = "傍晚"
      } else if (nowTime < 24 && nowTime >= 19) {
        timeGreet = "晚上"
      } else {
        timeGreet = "凌晨"
      }
      this.setData({
        userMsg: res.data,
        timeGreet: timeGreet
      })
    })
  },


  //返回上一页
  backPage() {
    wx.navigateBack()
  },

  //判断是否有上一页
  judgeHaveBackPage() {
    let pages = getCurrentPages()
    if (pages[pages.length - 2]) {
      this.setData({
        backBtn: true
      })
    } else {
      this.setData({
        backBtn: false
      })
    }
  },

  //以下是按钮拖动事件
  buttonStart: function (e) {
    startPoint = e.touches[0] //获取拖动开始点
  },

  buttonMove: function (e) {
    var endPoint = e.touches[e.touches.length - 1] //获取拖动结束点
    //计算在X轴上拖动的距离和在Y轴上拖动的距离
    var translateX = endPoint.clientX - startPoint.clientX
    var translateY = endPoint.clientY - startPoint.clientY
    startPoint = endPoint //重置开始位置
    var buttonBottom = this.data.buttonBottom - translateY
    var buttonRight = this.data.buttonRight - translateX
    //判断是移动否超出屏幕
    if (buttonRight + 50 >= this.data.windowWidth) {
      buttonRight = this.data.windowWidth - 50;
    }
    if (buttonRight <= 0) {
      buttonRight = 0;
    }
    if (buttonBottom <= 0) {
      buttonBottom = 0
    }
    if (buttonBottom + 50 >= this.data.windowHeight) {
      buttonBottom = this.data.windowHeight - 50;
    }
    this.setData({
      buttonBottom: buttonBottom,
      buttonRight: buttonRight
    })
  },
  buttonEnd: function (e) {},

  getAddress() {
    let that = this
    if (!app.globalData.lat) {
      wx.getLocation({
        success: function (e) {
          that.setData({
            lat: e.latitude,
            lng: e.longitude
          })

          app.globalData.lat = e.latitude
          app.globalData.lng = e.longitude
        },
        fail: function (err) {
          console.log('getLocation', err)
        }
      })
    } else {
      that.setData({
        lat: app.globalData.lat,
        lng: app.globalData.lng
      })
    }
  },


  onLoad(option) {

    this.setData({
      globalStype: app.globalData.globalStype,
      statusBarHeight: app.globalData.statusBarHeight
    })
    let shopId = ''
    if (app.globalData.shopId) {
      shopId = app.globalData.shopId
      wx.setStorageSync('shopId', shopId)
    }
    if (option.shopId) {
      console.log(option)
      shopId = option.shopId
      app.globalData.shopId = option.shopId
      wx.setStorageSync('shopId', shopId)

    }
    if (option.scene) {
      shopId = decodeURIComponent(option.scene).split(",")[0];
      app.globalData.shopId = shopId;
      wx.setStorageSync('shopId', shopId)
    }
    if (shopId) {
      this.setData({
        shopId
      })
      this.shopDetails()
    }
    if (option.invitationAccountId) {
      this.setData({
        invitationAccountId: option.invitationAccountId
      })
    }
    this.setData({
      tabbar: app.globalData.tabbar2,
    });
    this.onSubscribeMsg();
    let that = this

    //获取当前位置的经纬度
    this.getAddress()




  },
  onSubscribeMsg() {

  },
  onShow() {

    this.judgeHaveBackPage()
    if (wx.getStorageSync('usertoken')) {
      //加载首页数据
      this.getUserMsg()
      this.myInformation()
      // this.getShopDiscount()
      this.getCommentNum()
      // this.shopActivityPage()
      if (this.data.invitationAccountId) {
        this.invitationPerson()
      }
      this.getRedBagList()
    } else {
      //无usertoken，等待login登录回调
      app.usertokenReadyCallback = res => {
        if (res.data.sessionId) {
          this.getUserMsg()
          this.myInformation()
          // this.getShopDiscount()
          this.getCommentNum()
          // this.shopActivityPage()
          if (this.data.invitationAccountId) {
            this.invitationPerson()
          }
          this.getRedBagList()
        }
      }
    }

  },
  imageLoad: function (e) { //获取图片真实宽度  
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比  
      ratio = imgwidth / imgheight;
    // console.log(imgwidth, imgheight)
    //计算的高度值  
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight;
    var imgheights = this.data.imgheights;
    //把每一张图片的对应的高度记录到数组里  
    imgheights[e.target.dataset.id] = imgheight;
    this.setData({
      imgheights: imgheights
    })
    setTimeout(res => {
      this.getfixheight()
    }, 1000)
  },
  bindchange: function (e) {
    // console.log(e.detail.current)
    this.setData({
      current: e.detail.current
    })
  },
  tabChange(e) {
    var index = e.detail.index;
    if (index == 1) {
      wx.reLaunch({
        url: "/pages/myVipShop/myVipShop"
      });
    }
    if (index == 2) {
      wx.reLaunch({
        url: "/pages/my/my-friend/my-friend"
      });
    }
    if (index == 3) {
      wx.reLaunch({
        url: '/pages/my/my-index/my-index',
      })
    }
  },
  gochain() {
    wx.reLaunch({
      url: '/pages/Chain/Chain'
    })
  },
  shopDetails() {
    //获取数据
    Controllers.shopDetails({
      shopId: wx.getStorageSync('shopId')
    }).then(res => {
      if (res.data.label) {
        res.data.label = res.data.label.split('/')
      }
      console.log(res.data);
      this.setData({
        shopDetailsdata: res.data,
        openDiscount: res.data.discount,
        shopRotationImgdata: res.data.rotationImgList,
        shopRotationVideo: res.data.video,
      })

      if (this.data.shopRotationVideo) {
        // 创建video上下文
        this.video = wx.createVideoContext('shopRotationVideo', this)
      }


      console.log(new Date(res.data.shopGroupEndTime).getTime())
      if (!res.data.shopGroupEndTime || (new Date(res.data.shopGroupEndTime).getTime() < new Date().getTime())) {
        this.setData({
          tabList: [{
            name: "店铺推荐",
            id: 0
          }],
        })
      } else {
        this.setData({
          tabList: [{
            name: "店铺推荐",
            id: 0
          }],
        })
      }

      if (this.data.shopDetailsdata.typeName.indexOf('美业') != -1) {
        app.globalData.globalStype = 'pinStyle'
      } else {
        app.globalData.globalStype = 'yellowStyle'
      }
      this.setData({
        globalStype: app.globalData.globalStype
      })
      this.setData({
        tabbar: this.data.globalStype == 'pinStyle' ? app.globalData.tabbar : app.globalData.tabbar2,
      })
      this.shopGoodsPage()
    })
  },
  myInformation() {
    //获取数据
    Controllers.myInformation({
      shopId: this.data.shopId
    }).then(res => {
      this.setData({
        myInformationdata: res.data.accountRelation
      })
      app.globalData.myshopinfo = res.data.accountRelation
      wx.setStorageSync('accountId', res.data.id)
    })
  },
  getfixheight() {
    let query = wx.createSelectorQuery()
    var that = this;

    query.select('#store').boundingClientRect((rect) => {
      if (rect) {
        let top = rect.top;
        that.setData({
          navheight: top
        })
      }
    }).exec()

    let systemInfo = wx.getSystemInfoSync()
    console.log(systemInfo.statusBarHeight)
    let barheight = (750 / systemInfo.windowWidth) * systemInfo.statusBarHeight;
    let pxToRpxScale = 750 / systemInfo.windowWidth;
    let navigationHeight = 44 * pxToRpxScale;
    this.setData({
      headerheight: navigationHeight,
      barheight
    })
  },

  onUnload() {

  },


  changegoods(e) {
    var type = e.currentTarget.dataset.type
    this.setData({
      pageNumber: 1
    })
    this.setData({
      type
    })
    if (type == 0) {
      // this.shopGoodsPage()
    } else {
      this.cloutGoodsPage()
    }
  },


  onReachBottom() {
    if (this.data.selectIndex == 0 || this.data.selectIndex == 1) {
      this.getMore()
    } else {
      this.getCommentMore()
    }
  },


  onPageScroll: function (e) { // 获取滚动条当前位置
    if (this.data.navheight > 0) {

      if (this.data.navheight < e.scrollTop + 58) {
        this.setData({
          isFixedTop: true,
        })
        // console.log(this.data.navheight - e.scrollTop - 58)
      } else {
        this.setData({
          isFixedTop: false
        })
      }

    }
  },
  tolists() {
    wx.navigateTo({
      url: "/pages/Index/Lists?name=" + this.data.shopDetailsdata.shopName
    });
  },
  gointroduce() {
    wx.navigateTo({
      url: "/pages/Index/Introduce"
    });
  },
  onShareAppMessage: function (e) {
    let obj = {
      title: this.data.shopDetailsdata.shopName,
      path: '/pages/Index/Index?shopId=' + this.data.shopId + "&invitationAccountId=" + this.data.myInformationdata.accountId + "&tankuan=1",

    }
    return obj
  },
  onShareTimeline: function (e) {
    let obj = {
      title: '',
      path: '/pages/Index/Index?shopId=' + this.data.shopId,
      success: function (res) {

      },
      fail(err) {

      }
    }
    return obj
  },

  //绑定上级
  invitationPerson() {
    let value = {}
    value.invitationAccountId = this.data.invitationAccountId
    value.shopId = wx.getStorageSync('shopId')
    Controllers.invitationPerson(value).then(res => {

    })
  },

  //获取红包弹窗
  getRedBagList() {
    controller.getRedbag({
      shopId: this.data.shopId
    }).then(res => {
      this.setData({
        redBagList: res.data,
      })
      if (this.data.redBagList.length > 0) {
        this.setData({
          dialogShow: true,
          closeAnimate: true,
          hasRedBag: true
        })
      } else {
        this.setData({
          dialogShow: false,
          closeAnimate: true,
          receiveOut: true,
          hasRedBag: false
        })
      }

      this.getPoster()
    })
  },
  // 获取非红包弹窗
  getPoster() {
    Controllers.getPoster({
      shopId: this.data.shopId
    }).then(res => {
      this.setData({
        bulletImg: res.data
      })
      if (res.data) {
        this.setData({
          dialogShow: true,
          closeAnimate: true,
        })
      }
    })
  },

  //领取红包
  receiveRedBagBtn() {
    let that = this
    let value = {}
    if (!this.data.lng) {
      wx.showToast({
        icon: "none",
        title: '请回到首页重新获取地址授权',
      })
      return
    }
    value.redId = this.data.redBagList[0].id
    value.lng = this.data.lng
    value.lat = this.data.lat
    value.shopId = this.data.shopId
    controller.receiveRedBag(value).then(res => {
      wx.showToast({
        icon: "none",
        title: '领取成功',
      })
      this.setData({
        receiveOut: true,
        redMoney: res.data.price
      })
      // that.getRedBagList()
    })
  },



  //关闭弹窗
  cancleBullet() {
    this.setData({
      dialogShow: false,
      closeAnimate: false,
      receiveOut: false,
    })
    // this.getRedBagList()
    // wx.setStorageSync('tankuan', 2)
  },

  //点cancle图标关闭弹窗
  cancleIcon() {
    this.cancleBullet()
    this.getRedBagList()
  },

  //跳转我的收益
  toMyAccount() {
    this.setData({
      dialogShow: false,
      closeAnimate: false,
      receiveOut: false,
    })
    wx.navigateTo({
      url: '/pages/myCenter/myAccount',
    })
  },

  //跳转更多优惠活动
  toMoreDiscount() {
    this.setData({
      dialogShow: false,
      closeAnimate: false,
      receiveOut: false,
    })
    wx.navigateTo({
      url: '/pages/Mall/Coupon',
    })
  },


  toClickMe() {
    wx.navigateTo({
      url: '/pages/clickMe/clickMe',
    })
  },

  cancleClickMe() {
    this.setData({
      clickMe: false
    })
  },

  //获取商圈折扣是否开启
  // getShopDiscount() {
  //   controller.shopDiscount({
  //     shopId: this.data.shopId
  //   }).then(res => {
  //     this.setData({
  //       openDiscount: res.data.openDiscount
  //     })
  //   })
  // },

  //跳转商铺详情
  companyDetail() {
    wx.navigateTo({
      url: '/pages/Index/Introduce',
    })
  },

  //2.0UI
  //跳转积分商城
  toClickMy() {
    wx.navigateTo({
      url: '/pages/clickMe/clickMe',
    })
  },
  //跳转领券中心
  toCoupon() {
    let num = ""
    if (this.data.shopDetailsdata.shopCouponNum - this.data.myInformationdata.shopCouponNum > 0) {
      num = this.data.shopDetailsdata.shopCouponNum
    }
    wx.navigateTo({
      url: '/pages/Mall/Coupon?num=' + num,
    })
  },

  //跳转充值优惠与优惠套餐
  toVip(e) {
    let num = ""
    if (this.data.shopDetailsdata.shopVipNum - this.data.myInformationdata.shopVipNum > 0) {
      num = this.data.shopDetailsdata.shopVipNum
    }
    wx.navigateTo({
      url: '/pages/my/vip/vip_myCard/index?num=' + num,
    })
  },

  //跳转会员开卡
  toOpenVip(e) {
    let type = e.currentTarget.dataset.type
    if (type == 1) {
      wx.navigateTo({
        url: '/pages/my/vip/index?types=1,5',
      })
    } else {
      wx.navigateTo({
        url: '/pages/my/vip/index?types=0',
      })
    }
  },


  //跳转拼团活动
  toPinList() {
    wx.navigateTo({
      url: '/pages/Mall/Group',
    })
  },

  //跳转云商城
  toCloudMall() {
    wx.navigateTo({
      url: '/pages/Mall/cloudMall',
    })
  },

  //跳转预约
  toService() {
    wx.navigateTo({
      url: '/pages/Index/Service',
    })
  },

  //跳转活动详情页
  toActivityDetail(e) {
    let type = e.currentTarget.dataset.type;
    let num = ""
    if (type == 5 && (this.data.shopDetailsdata.groupGiftNum - this.data.myInformationdata.groupGiftNum > 0)) {
      num = this.data.shopDetailsdata.groupGiftNum
    } else if (type == 2 && (this.data.shopDetailsdata.groupHotNum - this.data.myInformationdata.groupHotNum > 0)) {
      num = this.data.shopDetailsdata.groupHotNum
    } else if (type == 4 && (this.data.shopDetailsdata.groupLimitNum - this.data.myInformationdata.groupLimitNum > 0)) {
      num = this.data.shopDetailsdata.groupLimitNum
    } else if (type == 3 && (this.data.shopDetailsdata.groupPinNum - this.data.myInformationdata.groupPinNum > 0)) {
      num = this.data.shopDetailsdata.groupPinNum
    }
    wx.navigateTo({
      url: '/pages/Index/activityList?type=' + type + '&num=' + num,
    })

  },

  //获取首页的商品
  shopGoodsPage() {
    let that = this
    Controllers.shopGoodsRecommendPage({
      pageSize: 5,
      pageNumber: this.data.pageNumber,
      shopId: this.data.shopId,
      openActivity: 0
    }).then(res => {
      this.setData({
        shopGoodsPagedata: res.data.list,
        lastPage: res.data.lastPage
      })
      // 防止不同列表覆盖
      if (this.data.listType == 0) {
        that.waterfallFlow(res.data.list)
      }

    })
  },

  //瀑布流布局
  async waterfallFlow(list) {
    let leftList = this.data.leftList
    let rightList = this.data.rightList
    let changeType = this.data.selectIndex
    for (let item of list) {
      // 防止切换列表，for循环还未结束，item添加到下一个列表中
      if (changeType != this.data.selectIndex) return
      console.log(leftHeight, rightHeight);
      leftHeight <= rightHeight ? leftList.push(item) : rightList.push(item); //判断两边高度，来觉得添加到那边
      await this.getBoxHeight(leftList, rightList);
    }
  },
  getBoxHeight(leftList, rightList) {

    return new Promise((resolve, reject) => {
      this.setData({
        leftList,
        rightList
      });
      let query = wx.createSelectorQuery();
      query.select('.left-item').boundingClientRect();
      query.select('.right-item').boundingClientRect();
      // 图片加载完成后再获取列表高度，防止数据在一边
      let time = setInterval(() => {
        if (this.data.imageLoaded) {
          this.setData({
            imageLoaded: false
          })
          query.exec((res) => {
            leftHeight = res[0].bottom; //获取左边列表的高度
            rightHeight = res[1].bottom; //获取右边列表的高度
            resolve();
          });
          clearInterval(time)
        }
      }, 10)

    })
  },
  // 图片加载完成
  imageLoad() {
    console.log('imageLoad');
    this.setData({
      imageLoaded: true,
    })
  },

  getMore() {
    let that = this
    if (this.data.lastPage == false) {
      let value = {}
      if (this.data.selectIndex == 0) {
        value.pageNumber = this.data.pageNumber + 1
        this.setData({
          pageNumber: value.pageNumber
        })
        value.shopId = this.data.shopId
        value.pageSize = 5
        value.openActivity = 0
        Controllers.shopGoodsRecommendPage(value).then(res => {
          let list = this.data.shopGoodsPagedata
          let list2 = res.data.list
          let list3 = list.concat(list2)
          this.setData({
            shopGoodsPagedata: list3,
            lastPage: res.data.lastPage
          })
          that.waterfallFlow(res.data.list)
        })
      } else {
        value.pageNumber = this.data.pageNumber + 1
        this.setData({
          pageNumber: value.pageNumber
        })
        value.shopId = this.data.shopId
        value.pageSize = 5
        Controllers.shopActivityPage(value).then(res => {
          let list = this.data.shopGoodsPagedata
          let list2 = res.data.list
          let list3 = list.concat(list2)
          this.setData({
            activityList: list3,
            lastPage: res.data.lastPage
          })
          that.waterfallFlow(res.data.list)
        })
      }
    }
  },

  toActDetail(e) {
    let type = e.currentTarget.dataset.type
    let id = e.currentTarget.dataset.id
    let shopGroupId = e.currentTarget.dataset.shopgroupid
    wx.navigateTo({
      url: '/pages/TradingArea/GiftBagDetail?id=' + id + "&shopGroupId=" + shopGroupId + "&type=" + type,
    })
  },

  //跳转点单/vip专享/股东专享
  toOrder(e) {
    let type = e.currentTarget.dataset.type
    // vip ==0普通用户1vip会员2普通会员3股东4代理商的用户身份
    let vip = this.data.myInformationdata.vip

    if (type == 'shareholder' && (vip != 3 && vip != 4)) {
      wx.showToast({
        title: '只有股东用户才能进入',
        icon: 'none',
        duration: 1500
      });
    } else if (type == 'vip' && (vip == 0 || vip == 2)) {
      wx.showToast({
        title: '只有VIP用户或者股东用户才能进入',
        icon: 'none',
        duration: 1500
      });
    } else {
      wx.navigateTo({
        url: '/pages/Mall/Mall?type=' + type,
      })
    }

  },






})